package com.example.demo;

import com.demo0329.sys.entity.User;
import com.demo0329.sys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/*
@RestController
@RequestMapping("/user")

public class UserController {
    @Autowired(required=false)
    private IUserService userService;

    @GetMapping("/all")
    public Result<List<User>> getAllUser(){
        List<User> list = userService.list();
        return Result.success(list,"查询成功");
    }

}*/

@RequestMapping("/user")
// @CrossOrigin   跨域处理
public class UserController {
    @Autowired
    private IUserService userService;
    @PostMapping("/login")  //post请求
    public Result<Map<String,Object>> login(@RequestBody User user){  //RequestBody将json格式转换
        Map<String,Object> data = userService.login(user);  //userService接口里实现login判断逻辑
        if(data != null){
            return Result.success(data);
        }
        return Result.fail(20002,"用户名或密码错误");
    }
    @GetMapping("/info")
    public Result<Map<String,Object>> getUserInfo(@RequestParam("token") String token){
        // 根据token获取用户信息，redis
        Map<String,Object> data = userService.getUserInfo(token);
        if(data != null){
            return Result.success(data);
        }
        return Result.fail(20003,"登录信息无效，请重新登录");
    }

    @PostMapping("/logout")//清除Redis里的token
    //登录后，每次访问请求头都会带上token，从请求头里获取token
    public Result<?> logout(@RequestHeader("X-Token") String token){
        userService.logout(token);
        return Result.success();
    }
}

